﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using SharePointOptimizer.SQLServer;
using System.Collections.ObjectModel;
using SharePointOptimizer.Model;
using System.Windows.Threading;
using SharePointOptimizer.BL.DataAnalysis;
using SharePointOptimizer.PL.WPF;
using SharePointOptimizer.PL.WPF.Properties;
using AG.Utilities;

namespace SharePointOptimizer.PL.WPF
{

    public delegate List<Exception> RunCollectData();
    public delegate void ProgressUpdateEvent(String Task, Decimal Status);
    public delegate void ProccesCompleted();
    public delegate void IssueCheckStart();

    public class DataCollectionWorker : AsyncUtils.AsyncOperation
    {

        public event ProgressUpdateEvent ProgressUpdate;
        public event ProccesCompleted ProccessCompleted;
        public event IssueCheckStart IssueCheckStart;
        public int countEventsCalls = 0;
        public static List<Exception> errors;
        public Decimal Status;

        public DataCollectionWorker(DispatcherObject target)
            : base(target)
        {
        }

        public void Fail()
        {
            lock (this)
            {
                _fail = true;
            }
        }
        private bool _fail;
        protected override void DoWork()
        {
            lock (this)
            {
                try
                {

                    (new CollectSharePointFarmData()).ClearAll();

                    List<Exception> errors = new List<Exception>();

                    if (!CancelRequested)
                    {

                        FireProgressUpdateEvent("Collecting Farm and Server Data", 25);
                    }

                    errors.AddRange((new CollectSharePointFarmData()).Collect());

                    if (!CancelRequested)
                    {

                        FireProgressUpdateEvent("Collecting Content Data", 80);
                    }

                    errors.AddRange((new CollectSharePointContentData()).Collect());
                    errors.AddRange((new CollectSharePointUsageData()).Collect());

                    FireProccessCompletedEvent();


                    if (CancelRequested)
                    {
                        AcknowledgeCancel();
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error(ex);
                }
            }
        }
        private void FireProgressUpdateEvent(String Task, Decimal status)
        {
            Object[] param = new Object[] { Task, status };

            FireAsync(ProgressUpdate, param);
        }

        private void FireProccessCompletedEvent()
        {
            (new CheckSharePointFarmForIssues()).RunIssueChecks();
            FireAsync(ProccessCompleted);

        }

        // Property changed event handler         
    }
}
